package it.univaq.mwt.model.entity;


import java.io.Serializable;
import java.util.Set;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.JoinColumn;

@Entity
@NamedQueries({ 
	@NamedQuery(name="getServices",query="SELECT s FROM Services s JOIN s.groups g WHERE g.id=:id_group"),
	@NamedQuery(name="getServicesList",query="SELECT s FROM Services s ORDER BY s.id ASC"),
@NamedQuery(name="searchNumServices",query="SELECT COUNT(s) FROM Services s"),
@NamedQuery(name="getServicesByPosition", query="SELECT s FROM Services s JOIN s.groups g WHERE g.id=:id_group AND s.position=:position")
	
})
@SequenceGenerator(name="sequence",sequenceName="MASTER.SERVICES_SEQ")
public class Services implements Serializable {
	@Id
	@GeneratedValue(generator="sequence")
    
	@Column(name="id_service",nullable=false,length=11)
	private int id;
	
	@Column(name="position",nullable=false,length=2)
	private int position;
	
	@Column(name="script",nullable=false, length=100)
	private String script;
	
	
	@Column(name="name",nullable=true, length=100)
	private String name;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	//@ManyToMany(mappedBy="services",cascade = { CascadeType.MERGE, CascadeType.REFRESH},fetch=FetchType.EAGER)
	@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER) 
	@JoinTable(name="services_groups",joinColumns = {@JoinColumn(name="id_service", referencedColumnName="id_service", nullable=false)},
			inverseJoinColumns={@JoinColumn(name="id_group", referencedColumnName="id_group", nullable=false)})	
	private Set<Groups> groups;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public String getScript() {
		return script;
	}

	public void setScript(String script) {
		this.script = script;
	}

	public Set<Groups> getGroups() {
		return groups;
	}

	public void setGroups(Set<Groups> groups) {
		this.groups = groups;
	}

	

	
	
}
